/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

import Objets.Groupe;
import Objets.SuperPair;
import java.io.FileInputStream;
import net.jxta.discovery.DiscoveryService;
import net.jxta.document.AdvertisementFactory;
import net.jxta.document.MimeMediaType;
import net.jxta.exception.PeerGroupException;
import net.jxta.peergroup.PeerGroup;
import net.jxta.peergroup.PeerGroupFactory;
import net.jxta.pipe.InputPipe;
import net.jxta.pipe.PipeService;
import net.jxta.protocol.ModuleImplAdvertisement;
import net.jxta.protocol.PeerGroupAdvertisement;
import net.jxta.protocol.PipeAdvertisement;

/**
 *
 * @author Bart
 */
public class PremierSuperPair {

    static PeerGroup netPeerGroup = null;
    private DiscoveryService discoSvc;
    private PipeService pipe;
    private PipeAdvertisement pipeAdv;
    public static SuperPair prems;

    /**
     * main
     *
     * @param args command line args
     */
    public static void main(String args[]) {
        PremierSuperPair myapp = new PremierSuperPair();
        prems = new SuperPair("premier");
        myapp.startJxta();
        PeerGroup peerGroupe = null;
        peerGroupe = myapp.createGroup("accueil");
        Groupe groupe = new Groupe("accueil", prems, peerGroupe);
        prems.setMonGroupe(groupe);
        if (groupe.getPeerGroupe() != null) {
            prems.joinGroup(peerGroupe);
        } else {
            System.out.println("Impossible de joindre le groupe");
        }
        prems.run();
    }

    private void startJxta() {
        try {
            // create, and Start the default jxta NetPeerGroup
            netPeerGroup = PeerGroupFactory.newNetPeerGroup();

            // uncomment the following line if you want to start the app defined
            // the NetPeerGroup Advertisement (by default it's the shell)
            // at which case you must include jxtashell.jar in the classpath
            // in this case we want use jxta directly.

            // netPeerGroup.startApp(null);

        } catch (PeerGroupException e) {
            // could not instanciate the group, print the stack and exit
            System.out.println("fatal error : group creation failure");
            e.printStackTrace();
            System.exit(1);
        }

        discoSvc = netPeerGroup.getDiscoveryService();

        pipe = netPeerGroup.getPipeService();
        prems.setSuperPipe(pipe);
        prems.setPrivatePipe(pipe);
        prems.setGroupePipe(pipe);
        System.out.println("Reading in .adv");
        try {
            FileInputStream is1 = new FileInputStream("pipe_entree.adv");
            pipeAdv = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(MimeMediaType.XMLUTF8, is1);
            prems.setSuperPipeAdv(pipeAdv);
            is1.close();
            FileInputStream is2 = new FileInputStream("pipe_prive1.adv");
            pipeAdv = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(MimeMediaType.XMLUTF8, is2);
            prems.setPrivatePipeAdv(pipeAdv);
            is2.close();
            FileInputStream is3 = new FileInputStream("pipe_groupe1.adv");
            pipeAdv = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(MimeMediaType.XMLUTF8, is3);
            prems.setGroupePipeAdv(pipeAdv);
            is3.close();
        } catch (Exception e) {
            System.out.println("failed to read/parse pipe advertisement");
            e.printStackTrace();
            System.exit(-1);
        }
    }

    private PeerGroup createGroup(String nomGroupe) {
        PeerGroup pg;               // new peer group
        PeerGroupAdvertisement adv; // advertisement for the new peer group

        System.out.println("Creating a new group advertisement");

        try {
            // create a new all purpose peergroup.
            ModuleImplAdvertisement implAdv = netPeerGroup.getAllPurposePeerGroupImplAdvertisement();

            pg = netPeerGroup.newGroup(null, // Assign new group ID
                    implAdv, // The implem. adv
                    nomGroupe, // The name
                    "Osef"); // Helpful descr.

            // print the name of the group and the peer group ID
            adv = pg.getPeerGroupAdvertisement();
            net.jxta.peergroup.PeerGroupID GID = adv.getPeerGroupID();
            System.out.println("  Group = " + adv.getName()
                    + "\n  Group ID = " + GID.toString());

        } catch (Exception eee) {
            System.out.println("Group creation failed with " + eee.toString());
            return (null);
        }

        try {
            // publish this advertisement
            // (send out to other peers and rendezvous peer)
            discoSvc.remotePublish(adv);
            System.out.println("Group published successfully.\n");
        } catch (Exception e) {
            System.out.println("Error publishing group advertisement");
            e.printStackTrace();
            return (null);
        }

        return (pg);

    }
}
